package com.hook3.pianke.activity.base.radiouserbasepager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.hook3.pianke.Adapter.PrivateRadioListBaseAdapter;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.R;
import com.hook3.pianke.domain.PrivateRadioInfo;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;


/**
 * 个人电台详情页
 */
public class RadioUserBaseInfoActivity extends AppCompatActivity {

    //个人电台id
    private String privateRadio;
//    private ImageView mSignboard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_user_base_info);

        //得到intent中的信息
        Intent intent = getIntent();
        privateRadio = intent.getStringExtra("radioid");
        String title = intent.getStringExtra("title");


        Toolbar toolbar = (Toolbar) findViewById(R.id.tb_ting_list_toolbar);

        toolbar.setTitle(title);
        setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //初始界面
        initView();


        //初始化data
        initData();


    }

    /**
     * 初始化界面
     */
    private ListView mPrivateRadioList;
    private LinearLayout mPrivateRadioListHeader;
    private ImageView mSignboard;
    private ImageView  mIcon;
    private TextView mName;
    private TextView mDes;
    private TextView mVisitNum;
    private void initView() {
        mPrivateRadioList = (ListView) findViewById(R.id.lv_radio_private_user_ting_list);
        mPrivateRadioListHeader = (LinearLayout) View.inflate(this, R.layout.ll_radio_private_user_list_header, null);

        mSignboard = (ImageView) mPrivateRadioListHeader.findViewById(R.id.iv_private_radio_list_signboard);
        mIcon = (ImageView ) mPrivateRadioListHeader.findViewById(R.id.iv_private_radio_list_Myicon);
        mName = (TextView) mPrivateRadioListHeader.findViewById(R.id.tv_private_radio_list_name);
        mDes = (TextView) mPrivateRadioListHeader.findViewById(R.id.tv_private_radio_list_des);
        mVisitNum = (TextView) mPrivateRadioListHeader.findViewById(R.id.tv_private_radio_list_visitnum);



        //给list添加头布局
        mPrivateRadioList.addHeaderView(mPrivateRadioListHeader);

    }


    /**
     * 初始化data
     */
    private void initData() {
        getDataFromNet();

    }

    /**
     * 从网络获取数据
     */
    private PrivateRadioInfo privateRadioInfos;
    //给个人电台列表设置adapter
    ArrayList<PrivateRadioInfo.PrivateRadioEachContent> privateRadiolist;
    private void getDataFromNet() {
        RequestParams requestparams = new RequestParams(Url.PRIVATERADIO);
        requestparams.addBodyParameter("radioid",privateRadio);
        x.http().post(requestparams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                privateRadioInfos = gson.fromJson(result, PrivateRadioInfo.class);

                //给listView的header设置信息
                setHeaderData();

                privateRadiolist = privateRadioInfos.data.list;
                mPrivateRadioList.setAdapter(new PrivateRadioListBaseAdapter(
                        RadioUserBaseInfoActivity.this, privateRadiolist));

                //给adapter设置监听器
                mPrivateRadioList.setOnItemClickListener(new MyOnItemClickListener());

                //给listView设置scroll监听
                mPrivateRadioList.setOnScrollListener(new PrivateListOnScrollListener());



            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }

    /**
     * 给listView的header设置信息
     */
    private void setHeaderData() {
        PrivateRadioInfo.PrivateCurrentRadioInfo radioInfo = privateRadioInfos.data.radioInfo;

        //设置广播大图
        ImageOptions imageOptions = new ImageOptions.Builder().build();
        x.image().bind(mSignboard,radioInfo.coverimg,imageOptions);

        x.image().bind(mIcon,radioInfo.userinfo.icon,imageOptions);
        mName.setText(radioInfo.userinfo.uname);
        mDes.setText(radioInfo.desc);
        mVisitNum.setText(radioInfo.musicvisitnum);
    }

    /**
     * 给listView设置监听器
     */
    private class MyOnItemClickListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(RadioUserBaseInfoActivity.this,RadioViewPagerActivity.class);
            Bundle bundle = new Bundle();
            ArrayList<PrivateRadioInfo.PrivateRadioEachContent> subPrivateRadiolist = new ArrayList<PrivateRadioInfo.PrivateRadioEachContent>();
            for (int i = 0; i <lastVisiblePosition ; i++) {
                subPrivateRadiolist.add(privateRadiolist.get(i));
            }
            bundle.putSerializable("privateRadiolist",subPrivateRadiolist);
            intent.putExtras(bundle);
            intent.putExtra("position",position - 1);
            startActivity(intent);
        }
    }

    /**
     * 给listView设置scroll监听
     */
    int lastVisiblePosition;
    private class PrivateListOnScrollListener implements AbsListView.OnScrollListener{

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
//            switch (AbsListView.OnScrollListener.)
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int currentPosition = view.getLastVisiblePosition();
            if(lastVisiblePosition == 0){
                lastVisiblePosition = view.getLastVisiblePosition();
            }
            if(currentPosition > lastVisiblePosition){
                lastVisiblePosition = currentPosition;
            }
        }
    }
}


